﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;
using System.Web.Http.Results;
using ZHDJ.AppInterface.Common;
using ZHDJ.AppInterface.Models;
using ZHDJ.Core;
using ZHDJ.Core.Util;
using ZHDJ.Data;

namespace ZHDJ.AppInterface.Controllers
{
    public class PartyHistoryController : ApiController
    {
        #region 获取党史
        [HttpPost]
        public ReturnResult GetPartyHistory(PartyHistory obj)
        {
            DataTable dt = new DataTable();
            IList<PartyHistory> list = new List<PartyHistory>();
            int month = Convert.ToInt32(obj.Month);
            int day = Convert.ToInt32(obj.Day);

            dt = InterfaceHelper.GetPartyHistory(month, day);
            if (dt.Rows.Count > 0)
                list = DataTableToListModel<PartyHistory>.ConvertToModel(dt);

            return new ReturnResult()
            {
                code = "200",
                result = list,
                msg = dt.Rows.Count.ToString()
            };
        }
        #endregion

        #region 获取总访问人数
        [HttpPost]
        public ReturnResult GetAllPartyHistroyCount()
        {
            int count = InterfaceHelper.GetPartyHistroyCount().Rows.Count;
            return new ReturnResult()
            {
                code = "200",
                result = count.ToString()
            };
        }
        #endregion

        #region 获取这一天的信息
        [HttpPost]
        public ReturnResult GetTodayPartyHistroyCount(PartyHistoryCount model)
        {
            double allCount = InterfaceHelper.GetPartyHistroyCount().Rows.Count;//总人数
            double todayCount = InterfaceHelper.GetPartyHistroyCountByWhere(model).Rows.Count;//当天入党人数
            PartyHistoryCountModel viewModel = new PartyHistoryCountModel();
            viewModel.TodayCount = Convert.ToInt32(todayCount);
            viewModel.Percentage = Math.Round(todayCount / allCount * 100.0, 2).ToString();

            return new ReturnResult()
            {
                code = "200",
                result = viewModel
            };
        }
        #endregion

        #region 添加访问人
        [HttpPost]
        public ReturnResult AddPartyHistroyCount(PartyHistoryCount model)
        {
            int i = InterfaceHelper.AddPartyHistroyCount(model);
            return new ReturnResult()
            {
                code = "200",
                result = i.ToString()
            };
        }
        #endregion
    }
}
